package com.swmaestro.twar;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;

import com.swmaestro.twar.model.FeedList;
import com.swmaestro.twar.task.URLs;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class TWarActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void onResume() {
    		super.onResume();
    		
    		try {
    			String url = URLs.URL_FEEDLIST;
//    			Log.d("test", "Server Request: " + url);
    			String response;
    			
    			HttpClient client = new DefaultHttpClient();
    			HttpGet get = new HttpGet(url);
    			HttpResponse responseGet = client.execute(get);
    			HttpEntity resEntityGet = responseGet.getEntity();
    			if(resEntityGet != null) {
//    				response = new String(EntityUtils.toByteArray(resEntityGet), "utf-8");
    	 			response = EntityUtils.toString(resEntityGet);
    				Log.e("test", "Server Response: " + response);
    	            
    	 			JSONArray ja = new JSONArray(response);
    	 			FeedList feedList = new FeedList(ja);
    	 			FeedList.log("test", feedList);
    	 			
    	 			//unicode test
//    	 			Toast.makeText(this, feedList.list[0].name, Toast.LENGTH_LONG).show();
    			} else {
    				//?
    			}
    		} catch (JSONException e) {
    			e.printStackTrace();
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    			//?
    		}
    		
    }
}